﻿// ———————————————————————–
// <copyright file="USNGCoordinateType.cs" company="The MITRE Corporation">
//    Copyright (c) 2014 The MITRE Corporation. All rights reserved.
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// </copyright>
// ———————————————————————–
/////////////////////////////////////////////////////////////////////////////////////////////////
// USNGCoordinateType.cs:
//
// A data type for United States National Grid (USNG) which represents a location using the Universal Transverse Mercator (UTM) and the UPS grid systems.
//
// Project: IC.NET - CAD Library
// Author:  Jim Cronin - MITRE
/////////////////////////////////////////////////////////////////////////////////////////////////

// Copyright (c) 2014 The MITRE Corporation. All rights reserved.
//
// NOTICE
// This software was produced for the U. S. Government
// under Contract No. FA8721-09-C-0001, and is
// subject to the Rights in Noncommercial Computer Software
// and Noncommercial Computer Software Documentation Clause
// (DFARS) 252.227-7014 (JUN 1995)

using System;
using System.Xml;
using EDXLSharp.NIEMCore;

namespace EDXLSharp.NIEMCADLib
{
  /// <summary>
  /// A data type for United States National Grid (USNG) which represents a location using the Universal Transverse Mercator (UTM) and the UPS grid systems.
  /// </summary>
  public abstract class USNGCoordinateType : ComplexTypeBase
  {
    /// <summary>
    /// An identifier of a USNG coordinate.
    /// </summary>
    public string CoordinateID { get; set; }

    /// <summary>
    /// A distance east within a USNG zone.
    /// </summary>
    public int EastingValue { get; set; }

    /// <summary>
    /// A distance north within a USNG zone.
    /// </summary>
    public int NorthingValue { get; set; }

    /// <summary>
    /// A spatial reference system.
    /// </summary>
    public GeographicDatumText GeographicDatum { get; set; }

    /// <summary>
    /// An identifier of a USNG zone.
    /// </summary>
    public string GridZoneID { get; set; }

    /// <summary>
    /// An identifier of a specific 100,000 meter squared region within a USNG zone.
    /// </summary>
    public string GridZoneSquareId { get; set; }

    /// <summary>
    /// Writes this Object to an inline XML Document
    /// </summary>
    /// <param name="xwriter">Valid XMLWriter</param>
    /// <exception cref="System.ArgumentNullException">Thrown if xwriter is null</exception>
    /// <exception cref="EDXLSharp.ValidationException">Thrown if the object fails to validate</exception>
    public override void WriteXML(XmlWriter xwriter)
    {
      if (xwriter == null)
      {
        throw new ArgumentNullException("xwriter");
      }

      this.Validate();

      xwriter.WriteStartElement(ElementName, ElementNamespace);
      xwriter.WriteElementString("USNGCoordinateID", EDXLConstants.EMEventNamespace, CoordinateID);
      xwriter.WriteElementString("USNGEastingValue", EDXLConstants.EMEventNamespace, EastingValue.ToString());
      xwriter.WriteElementString("USNGNorthingValue", EDXLConstants.EMEventNamespace, NorthingValue.ToString());
      GeographicDatum.WriteXML(xwriter);
      xwriter.WriteElementString("USNGGridZoneID", EDXLConstants.EMEventNamespace, GridZoneID);
      xwriter.WriteElementString("USNGGridZoneSquareID", EDXLConstants.EMEventNamespace, GridZoneSquareId);
      xwriter.WriteEndElement();
    }

    /// <summary>
    /// Reads this Object's values from an XML nodelist
    /// </summary>
    /// <param name="rootnode">root XML Node of the Object element</param>
    /// <exception cref="System.ArgumentNullException">Thrown if rootnode is null</exception>
    /// <exception cref="System.ArgumentException">Thrown if the rootnode is not the correct type</exception>
    /// <exception cref="EDXLSharp.ValidationException">Thrown if the object fails to validate</exception>
    public override void ReadXML(XmlNode rootnode)
    {
      if (rootnode == null)
      {
        throw new ArgumentNullException("rootnode");
      }

      if (rootnode.LocalName != ElementName)
      {
        throw new ArgumentException("Unexpected element " + rootnode.LocalName + " instead of " + ElementName);
      }

      foreach (XmlNode node in rootnode.ChildNodes)
      {
        switch (node.LocalName)
        {
          case "USNGCoordinateID":
            CoordinateID = node.InnerText;
            break;
          case "USNGEastingValue":
            EastingValue = int.Parse(node.InnerText);
            break;
          case "USNGNorthingValue":
            NorthingValue = int.Parse(node.InnerText);
            break;
          case "GeographicDatumText":
            GeographicDatum = new GeographicDatumText();
            GeographicDatum.ReadXML(node);
            break;
          case "USNGGridZoneID":
            GridZoneID = node.InnerText;
            break;
          case "USNGGridZoneSquareID":
            GridZoneSquareId = node.InnerText;
            break;
          default:
            throw new ArgumentException("Unexpected element " + node.LocalName + " in " + ElementName);
        }
      }

      Validate();
    }

    /// <summary>
    /// Validates This Message element For Required Values and Conformance
    /// </summary>
    /// <exception cref="EDXLSharp.ValidationException">Thrown if the object fails to validate</exception>
    public override void Validate()
    {
      if (string.IsNullOrEmpty(CoordinateID))
      {
        throw new ValidationException("USNGCoordinateID empty or missing");
      }

      if (EastingValue < 0)
      {
        throw new ValidationException("USNGEastingValue must not be negative");
      }

      if (NorthingValue < 0)
      {
        throw new ValidationException("USNGNorthingValue must not be negative");
      }

      if (GeographicDatum == null)
      {
        throw new ValidationException("Missing required field GeographicDatumText");
      }
      GeographicDatum.Validate();

      if (string.IsNullOrEmpty(GridZoneID))
      {
        throw new ValidationException("USNGGridZoneID empty or missing");
      }

      if (string.IsNullOrEmpty(GridZoneSquareId))
      {
        throw new ValidationException("USNGGridZoneSquareID empty or missing");
      }
    }
  }
}